#include "userMain.h"
#include "userKeypad.h"


#define		KEY_PRESS		0x01
#define		KEY_REALEASE	0x00

uint8_t AKeyValue = KEY_REALEASE;
uint8_t BKeyValue = KEY_REALEASE;
//uint8_t CKeyValue = KEY_REALEASE;
uint8_t KeyValueTemp = NONE_KEY;
uint8_t KeyValuePre = NONE_KEY;
uint8_t KeyValueNow = NONE_KEY;
uint8_t KeyRightCount = 0x00;


void userKeypadInitFunc(void)
{
#ifdef		USER_KEYPAD
    GPIO_InitTypeDef        GPIO_InitStructure;
	/* GPIOA Periph clock enable */
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	
	/* Configure PA0 and PA1 in output pushpull mode */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	_NOP;
#endif	
}

void userKeypadScanFunc(void)
{
#ifdef		USER_KEYPAD
    KeyValueNow = GPIO_ReadInputData(GPIOA);
    KeyValueNow &= 0x03;
	if((KeyValueNow == KeyValueTemp)&&(KeyValueNow == KeyValuePre))
	{
		KeyRightCount++;
		if(KeyRightCount>1)
		{
			userKeyValueSetFunc();
			KeyValueTemp = KeyValueNow;
			KeyValuePre = KeyValueNow;
			KeyRightCount = 0;
		}
		else
		{
			KeyValueTemp = KeyValueNow;
			KeyValuePre = KeyValueTemp;
		}
	}
	else 
	{
		KeyRightCount = 0;
		KeyValueTemp = KeyValueNow;
		KeyValuePre = KeyValueTemp;	 
	}
	_NOP;
#endif		
}

void userKeyValueSetFunc(void)
{
#ifdef		USER_KEYPAD
    AKeyValue = KEY_REALEASE;
    BKeyValue = KEY_REALEASE;
    //CKeyValue = KEY_REALEASE;
	switch(KeyValueNow)
	{
		case 0x02:
		{
            AKeyValue = KEY_PRESS;
			break;
		}
		case 0x01:
		{
			BKeyValue = KEY_PRESS;
			break;
		}
		default:   
		{
			break;
		}
	}
	_NOP;
#endif		
}
//*******************
uint16_t userResetKeyMarkTick = 0;
uint16_t userResetKeyMarkTicknNow;
uint8_t userResetKeyDealFlag = 0;
extern uint8_t userKeyValue;
void userResetKeyTick(void)
{
  if(GPIO_ReadInputData(GPIOA) & 0x04)  return ;
  if(userResetKeyMarkTick < 6000)
      userResetKeyMarkTick++;
}
void userResetKeyScan(void)
{
    uint8_t userResetKeyValue;
    userResetKeyValue = GPIO_ReadInputData(GPIOA);
    userResetKeyValue &= 0x04;
	
    if(userResetKeyValue)
    {
        userResetKeyMarkTicknNow = userResetKeyMarkTick;
        if( (userResetKeyMarkTicknNow>150) && (userResetKeyMarkTicknNow<300))
        {
            userResetKeyDealFlag = 0;
            userResetKeyMarkTicknNow = 0;
            userKeyValue = 0x05;
            userSendKeyInfoFunc();
           
        }
        userResetKeyMarkTick = 0;
        userResetKeyDealFlag = 1;
        return ;
    }
	else if( (userResetKeyMarkTick>150) && (userResetKeyMarkTick<152))
		   {
			   
			   userBeepOnFunc();
            LED_DELAY;
            userBeepOffFunc();
            LED_DELAY;
            userBeepOnFunc();
            LED_DELAY;
            userBeepOffFunc();
			 
		   }

	
    if(userResetKeyMarkTick > 500)
    {
        if(userResetKeyDealFlag)
        {
            _NOP;
            userResetKeyDealFlag = 0;
            userKeyValue = 0x06;
            userSendKeyInfoFunc();
            userBeepOnFunc();
            LED_DELAY;
            LED_DELAY;
            userBeepOffFunc();
        }
    }
    
}



/*****************END OF FILE*******************/

